from pathlib import PurePath
from typing import Sequence

from common.common_consts import AGENT_OTP_ENVIRONMENT_VARIABLE
from common.types import AgentID
from infection_monkey.exploit import IAgentOTPProvider
from infection_monkey.model import CMD_PREFIX, DROPPER_ARG, RUN_MONKEY, SET_OTP_WINDOWS
from infection_monkey.utils.commands import build_monkey_commandline


def build_rdp_command(
    agent_id: AgentID,
    servers: Sequence[str],
    current_depth: int,
    remote_agent_binary_destination_path: PurePath,
    otp_provider: IAgentOTPProvider,
):
    set_agent_otp_command = SET_OTP_WINDOWS % {
        "agent_otp_environment_variable": AGENT_OTP_ENVIRONMENT_VARIABLE,
        "agent_otp": otp_provider.get_otp(),
    }

    monkey_params = build_monkey_commandline(
        agent_id, servers, current_depth + 1, remote_agent_binary_destination_path
    )
    monkey_execution_command = RUN_MONKEY % {
        "monkey_path": remote_agent_binary_destination_path,
        "monkey_type": DROPPER_ARG,
        "parameters": monkey_params,
    }
    return f"{set_agent_otp_command} {monkey_execution_command}"
